package com.tianming.vrd.mapper;

import com.tianming.vrd.entity.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {
    @Insert("insert into product values(null,#{title},#{author}," +
            "#{intro},#{url},0,0,null,#{categoryId})")
    void insert(Product product);

    //当查询的表里面的字段名和实体类中的属性名不一致时,
    //需要通过@Result注解写一个对应关系,不然数据会查询不到
    @Select("select id,title,author,view_count,like_count,url from product")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "like_count",property = "likeCount")
    List<Product> select();

    @Delete("delete from product where id=#{id}")
    void delete(int id);

    @Select("select id,title,view_count,like_count" +
            ",url from product where category_id=#{cid}")
    List<Product> selectByCid(int cid);

    @Select("select id,title,view_count,like_count" +
            ",url from product where title like concat('%',#{wd},'%') ")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "like_count",property = "likeCount")
    List<Product> selectByWd(String wd);


    @Select("select id,title,intro,url from product" +
            " order by view_count desc limit 0,4")
    List<Product> selectByView();

    @Select("select id,title,intro,url from product" +
            " order by like_count desc limit 0,4")
    List<Product> selectByLike();

}
